package nps.pay.servlet;

import nps.pay.config.NpsConfig;
import nps.pay.utils.EncryptUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class NotifyServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(1234);
        Map<String, Object> parameterMap = req.getParameterMap();
        Map parameterMap1 = req.getParameterMap();
        //可删除
        System.out.println("++++++++++++++++++++++++++++++++++");
        parameterMap1.keySet().stream().forEach(item -> {
            System.out.println(item + "---" + req.getParameter(item.toString()));
        });
        System.out.println("++++++++++++++++++++++++++++++++++");

        String orderId = req.getParameter("ORDER_ID");
        String signSource = orderId +
                req.getParameter("ORDER_AMT") + req.getParameter("ORDER_TIME") +
                req.getParameter("USER_ID") + req.getParameter("BUS_CODE") +
                req.getParameter("RESP_CODE") + req.getParameter("RESP_DESC");
        String sign = req.getParameter("SIGN");
        String verifySign = EncryptUtil.MD5(EncryptUtil.MD5(signSource, 1).toUpperCase() + NpsConfig.SIGN_SALT, 0).toUpperCase();
        if (sign.equals(verifySign)) {//验签通过
            if ("0000".equals(req.getParameter("RESP_CODE"))) {
                //
                System.out.println("支付 成功" + orderId);
            }
            resp.getWriter().write("success");
        }
    }
}
